/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import java.io.Serializable;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import model.Cliente;
import model.Cliente_DAO;

/**
 *
 * @author Man
 */
public class RegistrarClienteAction extends Action implements Serializable {

     String next = "";
    HttpSession session = null;//request.getSession(true

   Cliente_DAO cdao= new Cliente_DAO();
       Cliente clie = new Cliente();

    @Override



    public void run() throws ServletException, IOException {

        session = request.getSession();
        Cliente cli = null;
        try {
            
            if (!cdao.existe(Integer.parseInt(request.getParameter("Identificacion")))) {

                
        clie.setCodigo(Integer.parseInt(request.getParameter("Identificacion")));
        clie.setNombre(request.getParameter("Nombre"));
         clie.setNombre2(request.getParameter("Nombre1"));
          clie.setApellido(request.getParameter("Apellido"));
           clie.setApellido2(request.getParameter("Apellido1"));
            clie.setDireccion(request.getParameter("Direccion"));
             clie.setEmail(request.getParameter("email"));
             clie.setTelefono(request.getParameter("Telefono"));

               
                      cdao.crear(clie);

                      session.setAttribute("consulta", "true");

               
              
                    next = "/RegistraClienteAdmin.jsp";

              
            } else {

                next = "/RegistraClienteAdmin.jsp";
             
                session.setAttribute("consulta", "false");

            }
        } catch (Exception ex) {
            throw new ServletException("Error al consultar cliente: ");
        }



        RequestDispatcher rd = application.getRequestDispatcher(next);
        if (rd == null) {
            throw new ServletException("No se pudo encontrar " + next);
        }
        rd.forward(request, response);


    }

}
